package com.artevivo.dao;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import com.artevivo.entidades.Anyo;
import com.artevivo.entidades.Cliente;
import com.artevivo.entidades.Matricula;

@Repository
public class MatriculaDAO extends AbstractDAO<Matricula, Long> {

    public MatriculaDAO() {
        super();
    }

    @Autowired
    public MatriculaDAO(SessionFactory sessionFactory) {
        super(sessionFactory);
    }

    @Transactional
    public List<Cliente> findAllClientesMatriculados(Anyo anyo) {
        List<Cliente> clientes = new ArrayList<Cliente>();
        Session sesion = this.getSessionFactory().getCurrentSession();
        Criteria criteria = sesion.createCriteria(Matricula.class);
        criteria.add(Restrictions.eq("anyo", anyo));
        List<Matricula> matriculasAnyo = criteria.list();
        if (matriculasAnyo != null && !matriculasAnyo.isEmpty()) {
            for (Matricula m : matriculasAnyo) {
                clientes.add(m.getCliente());
            }
        }
        return clientes;
    }
}
